package com.yoomiito.app.ui.my.psw;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.utils.SoftKeyBoardUtils;
import com.yoomiito.app.widget.PasswordView_1;

import butterknife.BindView;

public class SetPayPswFragment extends BaseLazyFragment<SetPayPswPresenter> {

    @BindView(R.id.fm_input_psw_title)
    TextView titleTv;
    @BindView(R.id.fm_input_psw_subtitle)
    TextView subtitleTv;
    @BindView(R.id.fm_input_psw_psw)
    PasswordView_1 mPasswordView;
    @BindView(R.id.psw_result)
    TextView resultTv;
    //    @BindView(R.id.scrollView)
//    ScrollView mScrollView;
    @BindView(R.id.ll)
    LinearLayout ll;
    private String oldPsw;
    private boolean isFirst = true;

    @Override
    public int getLayoutId() {
        return R.layout.fm_input_pay_psw;
    }

    @Override
    public SetPayPswPresenter newP() {
        return new SetPayPswPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        SoftKeyBoardUtils.showView(ll,resultTv);
        mPasswordView.setTextChangedListener(new PasswordView_1.TextChangedListener() {
            @Override
            public void textChanged(CharSequence changeText) {
                resultTv.setVisibility(View.INVISIBLE);
            }

            @Override
            public void textCompleted(CharSequence text) {
                String psw = text.toString();
                if (isFirst) {
                    oldPsw = psw;
                    isFirst = false;
                    mPasswordView.clearText();
                    subtitleTv.setVisibility(View.INVISIBLE);
                    titleTv.setText("请再次输入");
                    return;
                }
                if (oldPsw.equals(psw)) {
                    resultTv.setVisibility(View.INVISIBLE);
                    showLoading();
                    getP().setPayPsw(psw);
                } else {
                    resultTv.setVisibility(View.VISIBLE);
                }
            }
        });
        mPasswordView.requestFocus();
        mPasswordView.setFocusableInTouchMode(true);
    }

    public void setSuccess(boolean result) {
        dismissLoading();
        ((BaseActivity)context).updateUserInfo();
        if (result) {
            if (mPasswordView != null) {
                mPasswordView.disSoftInput();
            }
            ((SetPayPswActivity) context).switchFm(2);
        } else {
            mPasswordView.clearText();
        }
    }

}
